package youleche.com.controler.controler;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import youleche.com.business.BArea;
import youleche.com.business.BFixInfo;
import youleche.com.business.BHistoryInfo;
import youleche.com.business.BRentVehicleInfo;
import youleche.com.business.BSeller;
import youleche.com.business.BUser;
import youleche.com.business.BVehicleImageInfo;
import youleche.com.business.BVehicleInfo;
import youleche.com.caches.BaseParamInfoCache;
import youleche.com.common.IDFactory;
import youleche.com.connection.ConnectionFactory;
import youleche.com.model.MArea;
import youleche.com.model.MBaseParamInfo;
import youleche.com.model.MFixInfo;
import youleche.com.model.MHistoryInfo;
import youleche.com.model.MRentVehicleInfo;
import youleche.com.model.MSeller;
import youleche.com.model.MUser;
import youleche.com.model.MVehicleImageInfo;

/**
 * 页面直接获取数据展示控制器
 * @author Administrator
 *
 */
public class PageDataForBussinessControler {
	/**
	 * 获取品牌总数
	 * @return 返回品牌总数
	 */
	public static int getVehicleNameForCount(){
		BVehicleInfo handler = new BVehicleInfo();
		try {
			return handler.getVehicleNameForCount();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 获取商家总数
	 * @return 返回商家总数
	 */
	public static int getSellerForCount(){
		BUser handler = new BUser();
		try {
			return handler.getSellerForCount();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 得到热门位置
	 * @param city 城市名称
	 * @return 返回热门位置
	 */
	public static List<MArea> getHotAreaByCity(String city){
		BArea handler = new BArea();
		List<MArea> list_denifine = null;
		try {			
			if("天津".equals(city) || "上海".equals(city) ||  "北京".equals(city) || "重庆".equals(city)){
				city = city + "市";
			}
			
			MArea area = handler.getAreaByAreaName(city);
			if(area != null){
				List<MArea> list = handler.getAreaByParentCode(area.getAreaCode());
				if(list != null && list.size() >0){
					List<MArea> firstList = null;
					List<MArea> totalList = new LinkedList<MArea>();
					for (MArea _area : list) {
						if(totalList.size() > 7){
							return totalList;
						}
						
						firstList = handler.getAreaByParentCode(_area.getAreaCode());
						if(firstList != null && firstList.size() > 7){
							return firstList;
						}else{
							for (MArea tempArea : firstList) {
								totalList.add(tempArea);
							}
						}
					}
				}else{
					list_denifine = getDenifineArea();
				} 
			}else{
				list_denifine = getDenifineArea();
			}
			
			return list_denifine;
		} catch (Exception e) {
			list_denifine = getDenifineArea();
			return list_denifine;
		}
	}
	
	/**
	 * 得到自定义的区域数据
	 * @return 自定义的区域数据
	 */
	public static  List<MArea> getDenifineArea(){
		List<MArea> list_denifine = new LinkedList<MArea>();
		String[] cityArr = new String[]{"北京", "上海", "广州", "深圳", "成都", "天津", "厦门"};
		MArea area_denifine = null;
		for (String city_denifine : cityArr) {
			area_denifine = new MArea();
			area_denifine.setAreaName(city_denifine);
			list_denifine.add(area_denifine);
		}
		
		return list_denifine;
	}
	
	/**
	 * 获取首页图片信息(轮播)
	 * @return 返回首页图片信息（只有4条）
	 * @throws Exception 异常
	 */
	public static List<MVehicleImageInfo> getHomeImageInfoForLunBo(){
		BVehicleImageInfo handler = new BVehicleImageInfo();
		try {
			List<MVehicleImageInfo> list = handler.getHomeImageInfo();
			if(list == null){
				return null;
			}
			
			List<MVehicleImageInfo> homelist = new LinkedList<MVehicleImageInfo>();
			int index = 0;
			for (MVehicleImageInfo img : list) {
				index ++;
				if(index == 5){
					break;
				}
				
				homelist.add(img);				
			}
			
			return homelist;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 生成车辆id
	 * @return
	 */
	public static  String generateVehicleId(){
		try{
			return IDFactory.getId("meileche", "vehicleInfo");
		}catch(Exception ex){
			return IDFactory.getId("meileche", "vehicleInfo"); 
		}
	}
	
	/**
	 * 通过车辆id获取车辆图片信息
	 * @param vehicleId 车辆id
	 * @return 返回图片信息
	 */
	public static List<MVehicleImageInfo> getImageInfoByVehicleId(String vehicleId){
		BVehicleImageInfo handler = new BVehicleImageInfo();
		try {
			return handler.getImageInfoByVehicleId(vehicleId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 当没有车辆图片的时候初始化图片
	 * @return 返回初始化图片信息
	 */
	public static List<MVehicleImageInfo> getInitImage(){
		MVehicleImageInfo imageInfo = new MVehicleImageInfo();
		imageInfo.setImagePath("nopic_200_200.jpg");
		imageInfo.setImageType("1");
		
		List<MVehicleImageInfo> list = new LinkedList<MVehicleImageInfo>();
		list.add(imageInfo);
		return list;
	}
	
	/**
	 * 获取基础参数
	 * @return 基础参数
	 */
	@SuppressWarnings("unchecked")
	public static List<MBaseParamInfo> getBaseParamInfo(){
		List<MBaseParamInfo> baseParams = null;
		try {
			baseParams = (List<MBaseParamInfo>) BaseParamInfoCache.getCache();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return baseParams;
	}
	
	/**
	 * 首次加载修理时获取修理信息12条
	 * @return 理信息12条
	 */
	public static List<MFixInfo> getFixInfoList(){
		List<MFixInfo> fixs = null;
		try{
			BFixInfo handler = new BFixInfo();
			fixs = handler.getFixInfos("0", "12");
		}catch(Exception e){
			
		}
		
		return fixs;
	}
	
	/**
	 * 根据用户Id得到用户认证图片
	 * @param userId 用户Id
	 * @return 认证图片地址
	 */
	public static String getSellerImageSrcByUserId(String userId){
		BSeller handler = new BSeller();
		try {
			MSeller seller = handler.getSellerByUserId(userId);
			if(seller == null){
				return "";
			}else{
				return seller.getShopImgUrl();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "";
	}
	
	/**
	 * 获取版本历程
	 * @return 返回版本历程数据
	 */
	public static List<MHistoryInfo> getHistoryInfoList(){
		BHistoryInfo handler = new BHistoryInfo();
		List<MHistoryInfo> list = null;
		try {
			list = handler.getHistoryList("0", "15");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}
	
	/**
	 * 初始化租车信息列表
	 * @return 租车信息列表
	 */
	public static List<MRentVehicleInfo> getRentInfos(){
		BRentVehicleInfo handler = new BRentVehicleInfo();
		try {
			return handler.getRentVehicleInfoForList("0", "10", " order by addTime desc ");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 针对首页初始化租车信息列表
	 * @return 租车信息列表
	 */
	public static List<MRentVehicleInfo> getRentInfosToHome(){
		BRentVehicleInfo handler = new BRentVehicleInfo();
		try {
			return handler.getRentVehicleInfoForList("0", "5", " order by addTime desc ");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 判断是否是商家
	 * @param userName 用户名
	 * @return 返回true  是商家
	 */
	public static boolean isSeller(String userName){
		BUser handler = new BUser();
		try {
			MUser user = handler.getUserByUserName(userName);
			if(user != null && !"".equals(user.getEmail())){
				if(handler.isSellserByEmail(user.getEmail())){
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return false;
	}
	
	/**
	 * 公用的添加cookie
	 * @param response
	 * @param cookieName
	 * @param cookieValue
	 */
	public static void setCookie(HttpServletResponse response, String cookieName, String cookieValue){
		Cookie cookie = new Cookie(cookieName,cookieValue);
		cookie.setMaxAge(90); //设置15分钟 
		response.addCookie(cookie);
	}
}
